<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->isLogin();
		$this->isItAdministrator();//不是超级管理员
		$this->load->model('User_model');
	}
	public function userList(){
		$where=array();
		if(isset($_POST['type'])){
			$where['type']=array('=',$_POST['type']);
		}
		if(isset($_POST['status'])){
			$where['status']=array('=',$_POST['status']);
		}
		if(isset($_POST['name'])){
			$where['name']=array('like',$_POST['name']);
		}
		if(isset($_POST['powerlist'])){
			$where['powerlist']=array('wstring',"powerlist & ".$_POST['powerlist']);
		}
		$userlist=$this->User_model->pm_user_list($where);
		$this->assign('list',$userlist);
		if(isset($_POST['sousou'])){
			$html=$this->fetch('user/list_ajax.html');
			ajax_return(1,'success',$html);
		}
		$this->display('user/list.html');
	}
	public function userAdd(){
		if(isset($_POST['name'])){
			$data=$_POST;
			if(trim($data['name'])==''){
				ajax_return(0,'登陆名称不能为空');
			}
			if(trim($data['pwd'])==''){
				ajax_return(0,'密码未设置');
			}
			if($data['pwd']!=$data['pwdok']){
				ajax_return(0,'两次密码设置不一样');
			}
			/* if($data['isitadministrator']==0 && !isset($data['powerlist'])){
				ajax_return(0,'不是超级管理员,请选择权限');
			} */
			if(isset($data['powerlist'])){
				$data['powerlist']=array_sum($data['powerlist']);
			}
			
			$data['pwd']=userpassmd5($data['pwd']);
			unset($data['pwdok']);
			$data['addtime']=time();
			//查询是否存在用户
			if($this->User_model->pm_user_onelist_name($data['name'])){
				ajax_return(0,'已经存在用户');
			}
			if($this->User_model->pm_user_add($data)===false){
				$keywords=$data['name']."添加失败";
				useraddrecord('用户添加',$keywords);
				ajax_return(0,'提交失败');
			}else{
				$keywords=$data['name']."添加成功";
				useraddrecord('用户添加',$keywords);
				ajax_return(1,'提交成功');
			}
		}else{
			$this->display('user/add.html');
		}
	}
	public function userEdit(){
		if(isset($_GET['id'])){
			//查询用户信息
			$list=$this->User_model->pm_user_onelist($_GET['id']);
			$this->assign('list',$list);
			$this->display('user/edit.html');
		}elseif($_POST['id']){
			$data=$_POST;
			if(isset($data['powerlist'])){
				$data['powerlist']=array_sum($data['powerlist']);
			}else{
				$data['powerlist']=null;
			}
			if(trim($data['name'])==''){
				ajax_return(0,'登陆名称不能为空');
			}
			if($data['pwd']!=$data['pwdok']){
				ajax_return(0,'两次密码设置不一样');
			}
			if($data['pwd']==''){//不修改密码
				unset($data['pwd']);
				unset($data['pwdok']);
			}else{
				unset($data['pwdok']);
				$data['pwd']=userpassmd5($data['pwd']);
			}
			/* if($data['isitadministrator']==0 && !isset($data['powerlist'])){
				ajax_return(0,'不是超级管理员,请选择权限');
			} */
			if($this->User_model->pm_user_edit($data)===false){
				$keywords=$data['name']."账号,编辑失败";
				useraddrecord('用户编辑',$keywords);
				ajax_return(0,'提交失败');
			}else{
				$keywords=$data['name']."账号,编辑成功";
				useraddrecord('用户编辑',$keywords);
				ajax_return(1,'提交成功');
			}
		}
	}
}
